/* 
 * Copyright [2018] [Alex/libo(liboware@gmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.infodb.file.service.directory.impl;

import java.util.Date;
import java.util.Optional;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import com.hyts.infodb.config.FileTypeEnum;
import com.hyts.infodb.config.StatusTypeEnum;
import com.hyts.infodb.error.subclass.DaoException;
import com.hyts.infodb.file.bean.directory.DirectoryBO;
import com.hyts.infodb.file.bean.directory.DirectoryPO;
import com.hyts.infodb.file.bean.directory.DirectoryVO;
import com.hyts.infodb.file.bean.file.FilePO;
import com.hyts.infodb.file.bean.file.FileVO;
import com.hyts.infodb.file.dao.directory.DirectoryDAO;
import com.hyts.infodb.file.dao.file.FileDAO;
import com.hyts.infodb.file.service.directory.DirectoryService;

/** 
 * @author LiBo/Alex
 * @see com.hyts.infodb.file.service.directory.DirectoryService
 * @since V1.0
 * @version V1.0 
 */
@Service
public class DirectoryServiceImpl implements DirectoryService {

    
    @Autowired
    private DirectoryDAO directoryDao;
    
    /* 
     * @param directory
     * @return
     * @see com.hyts.infodb.file.service.directory.DirectoryService#findDirectoryList(com.hyts.infodb.file.bean.directory.DirectoryBO)  
     * @exception
     */
    @Override
    public DirectoryVO findDirectoryList(DirectoryBO directory) {
        DirectoryVO vo = new DirectoryVO();
        Page<DirectoryPO> pageData = directoryDao.findAll(directory.buildPageData(directory.getPageNum(), directory.getPageSize()));
        vo.setContents(pageData.getContent());
        vo.setTotalDataSize(pageData.getTotalElements());
        vo.setTotalPageNumber(pageData.getTotalPages());
        return  vo;
    }

    /* 
     * @param directory
     * @return
     * @see com.hyts.infodb.file.service.directory.DirectoryService#loadDirectory(com.hyts.infodb.file.bean.directory.DirectoryBO)  
     * @exception
     */

    @Override
    public DirectoryVO loadDirectory(DirectoryBO directory) {
        DirectoryVO vo = new DirectoryVO();
        Optional<DirectoryPO> directoryObj = directoryDao.findById(directory.getId());
        if(directoryObj.isPresent()) {
            vo.setContent(directoryDao.findById(directory.getId()).get());
        }
        return vo;
    }

    /* 
     * @param directory
     * @return
     * @see com.hyts.infodb.file.service.directory.DirectoryService#saveDirectory(com.hyts.infodb.file.bean.directory.DirectoryBO)  
     * @exception
     */
    @Override
    public boolean saveDirectory(DirectoryBO directory) {
        DirectoryPO directoryPO = new DirectoryPO();
        BeanUtils.copyProperties(directory, directoryPO);
        if(directoryPO.getCreateDate() == null)
            directoryPO.setCreateDate(new Date());
        if(directoryPO.getIsHidden() == null)
            directoryPO.setIsHidden(StatusTypeEnum.SHOW.getValue());
        directoryPO.setEditDate(new Date());
        return Optional.ofNullable(directoryDao.save(directoryPO)).isPresent();
    }

    /* 
     * @param directory
     * @return
     * @see com.hyts.infodb.file.service.directory.DirectoryService#deleteDirectory(com.hyts.infodb.file.bean.directory.DirectoryBO)  
     * @exception
     */

    @Override
    public boolean deleteDirectory(DirectoryBO directory) {
        DirectoryPO directoryPO = new DirectoryPO();
        BeanUtils.copyProperties(directory, directoryPO);
        try {
            directoryDao.delete(directoryPO);
        } catch (Exception e) {
            throw new DaoException("删除目录失败!",e);
        }
        return true;
    }

}
